package com.deliveryservicesystem.app.validation;

import com.deliveryservicesystem.model.Subscription;
import com.deliveryservicesystem.model.Supplier;
import com.jgoodies.binding.value.AbstractValueModel;
import com.jgoodies.common.base.Strings;
import com.jgoodies.validation.ValidationResult;
import com.jgoodies.validation.Validator;

/**
 * File Name: ProductValidator.java
 * Version:
 * Description:
 * Package: com.deliveryservicesystem.app.validation
 * Author: Lance
 * Date: Jun 29, 2012
 * Editor:
 */
public class ProductValidator extends AbstractValueModel implements Validator<ValidationResult>
{

    private static final long serialVersionUID = 1L;
    private String txtProdPrice;
    private String txtProdDesc;
    private Supplier cbSupplierNo;
    private Subscription cbSubscriptionNo;

    public ProductValidator(String txtProdPrice, String txtProdDesc, Object cbSupplierNo, Object cbSubscriptionNo)
    {
        this.txtProdPrice = txtProdPrice;
        this.txtProdDesc = txtProdDesc;
        this.cbSupplierNo = (Supplier) cbSupplierNo;
        this.cbSubscriptionNo = (Subscription) cbSubscriptionNo;
    }

    public Subscription getCbSubscriptionNo()
    {
        return cbSubscriptionNo;
    }

    public void setCbSubscriptionNo(Subscription cbSubscriptionNo)
    {
        this.cbSubscriptionNo = cbSubscriptionNo;
    }

    public Supplier getCbSupplierNo()
    {
        return cbSupplierNo;
    }

    public void setCbSupplierNo(Supplier cbSupplierNo)
    {
        this.cbSupplierNo = cbSupplierNo;
    }

    public String getTxtProdDesc()
    {
        return txtProdDesc;
    }

    public void setTxtProdDesc(String txtProdDesc)
    {
        this.txtProdDesc = txtProdDesc;
    }

    public String getTxtProdPrice()
    {
        return txtProdPrice;
    }

    public void setTxtProdPrice(String txtProdPrice)
    {
        this.txtProdPrice = txtProdPrice;
    }

    @Override
    public Object getValue()
    {
        return this;
    }

    @Override
    public void setValue(Object newValue)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ValidationResult validate(ValidationResult validationTarget)
    {
        if (Strings.isEmpty(txtProdDesc.trim()))
        {
            validationTarget.addError("Product description required");
        }
        if (Strings.isEmpty(txtProdPrice.trim()))
        {
            validationTarget.addError("Product price required");
        }
        if (cbSupplierNo == null)
        {
            validationTarget.addError("Please select a supplier number");
        }
        if (cbSubscriptionNo == null)
        {
            validationTarget.addError("Please select a subscription number");
        }
        try
        {
            Double.parseDouble(txtProdPrice.trim());
        }
        catch (Exception e)
        {
            validationTarget.addError("Product price must be numeric");
        }
        return validationTarget;
    }
}
